package com.example.universita.esami;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Caricavoto extends Activity {
	public String items[] = new String[9];

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caricavoto);
        
        Button anno = (Button) findViewById(R.id.button1);
        Button corso = (Button) findViewById(R.id.button2);
        Button voto = (Button) findViewById(R.id.button3);
        
        boolean state = false;
        
        corso.setEnabled(state);
        voto.setEnabled(state);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_caricavoto, menu);
        return true;
    }
    
        
      public void OnCorsoClicked(View view) {
    	  
    	  TextView annos = (TextView) findViewById(R.id.textView1);
    	  String anno = annos.getText().toString();
    	  
    	  if(anno == "Primo"){
    		  items = new String[7];
    		  items[0] = "PR1";
    		  items[1] = "ARE";
    		  items[2] = "FDI";
    		  items[3] = "CDI";
    		  items[4] = "MD";
    		  items[5] = "FIS";
    		  items[6] = "AST";
    	  }
    	  else if(anno == "Secondo"){
    		  items = new String[9];
    		  items[0] = "ALF";
    		  items[1] = "S01";
    		  items[2] = "CSMN";
    		  items[3] = "EDI/E";
    		  items[4] = "EDI/D";
    		  items[5] = "RC";
    		  items[6] = "PR2";
    		  items[7] = "STI";
    		  items[8] = "AMM";
    	  }
    	  else if(anno == "Terzo"){
    		  items = new String[6];
    		  items[0] = "LIP";
    		  items[1] = "BD1";
    		  items[2] = "IUM";
    		  items[3] = "PSI";
    		  items[4] = "MR";
    		  items[5] = "ISW";
    	  }
    	  else if(anno == "Quarto"){
    		  items = new String[9];
    		  items[0] = "AS2";
    		  items[1] = "ERI";
    		  items[2] = "MEF";
    		  items[3] = "SO2";
    		  items[4] = "BD2";
    		  items[5] = "AN";
    		  items[6] = "MC";
    		  items[7] = "STI";
    		  items[8] = "CR";
    	  }
    	  else if(anno == "Quinto"){
    		  items = new String[4];
    		  items[0] = "DM";
    		  items[1] = "FS";
    		  items[2] = "ARE 2";
    		  items[3] = "RO";
    	  }
    	  
    	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	  builder.setTitle("Scegli un Esame");
    	  builder.setItems(items, new DialogInterface.OnClickListener() {
    		  
    	      public void onClick(DialogInterface dialog, int item) {
    	    	  
    	          Button voto = (Button) findViewById(R.id.button3);
    	          boolean state = true;
    	          
    	    	  TextView textview2 = (TextView) findViewById(R.id.textView2);
    	          Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    	          textview2.setText(items[item]);
    	          voto.setEnabled(state);
    	      }
    	  });
    	  AlertDialog alert = builder.create();
    	  alert.show();
      }
      
      public void OnAnnoClicked(View view) {
    	  
    	  final CharSequence[] items = {"Primo", "Secondo", "Terzo", "Quarto", "Quinto"};
	  
      
    	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	  builder.setTitle("Scegli l'anno");
    	  builder.setItems(items, new DialogInterface.OnClickListener() {
    		  
    	      public void onClick(DialogInterface dialog, int item) {
    	    	  
    	    	  Button corso = (Button) findViewById(R.id.button2);
    	    	  boolean state = true;
    	    	  TextView textview1 = (TextView) findViewById(R.id.textView1);
    	    	  
    	          Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    	          textview1.setText(items[item]);
    	    	  corso.setEnabled(state);
    	      }
    	  });
    	  AlertDialog alert = builder.create();
    	  alert.show();
      }
      
      public void OnVotoClicked(View view) {
    	  
    	  final CharSequence[] items = {"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "30 e lode"};
    	  
        	  AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	  builder.setTitle("Scegli il voto");
        	  builder.setItems(items, new DialogInterface.OnClickListener() {
        	      public void onClick(DialogInterface dialog, int item) {
        	    	  TextView textview3 = (TextView) findViewById(R.id.textView3);
        	          Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        	          textview3.setText(items[item]);
        	      }
        	  });
        	  AlertDialog alert = builder.create();
        	  alert.show();
          }
      
      public void OnSalvaClicked(View view){
    	  TextView textview1 = (TextView) findViewById(R.id.textView1);
    	  TextView textview2 = (TextView) findViewById(R.id.textView2);
    	  TextView textview3 = (TextView) findViewById(R.id.textView3);
    	  String anno = textview1.getText().toString();
    	  String corso = textview2.getText().toString();
    	  String voto = textview3.getText().toString();
    	  String no = "Anno: " +anno + "\n\nCorso: " +corso + "\n\nVoto: " +voto;
    	  Toast toast = Toast.makeText(getApplicationContext(), no, Toast.LENGTH_SHORT);
    	  toast.show();
      }
      
      /*public void onSaveClick(View view){
    	  
    	  String FILENAME = "esami";
    	  FileOutputStream fOut = null;
    	  OutputStreamWriter osw = null;
    	  
    	  try{
    	  fOut = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    	  
    	  //EditText edittext1 = (EditText) findViewById(R.id.editText1);
    	  //TextView textview3 = (TextView) findViewById(R.id.textView3);
    	  //String value = edittext1.getText().toString();
    	  //String boh = textview3.getText().toString();
    	  //String no = "-  " +boh + " :   " +value + "  -";
    	  
    	  String b = "Salvato";
    	  osw = new OutputStreamWriter(fOut);
    	  osw.write(b);
    	  osw.close();
    	  fOut.close();
    	  }catch(Exception e){
    		  e.printStackTrace(System.err); }
      }
      
      public void onViewClick(View view){
    	  
    	  String FILENAME = "esami";
    	  String data = null;
    	  TextView textview5 = (TextView) findViewById(R.id.textView5);
    	  
    	  FileInputStream fIn = null;
    	  InputStreamReader isr = null;
    	  
    	  try{
    	  
    	  char[] inputBuffer = new char[1024];
    	  
    	  fIn = openFileInput(FILENAME);
    	  isr = new InputStreamReader(fIn);
    	  isr.read(inputBuffer);
    	  data = new String(inputBuffer);
    	  textview5.setText(data);

    	  isr.close();
    	  fIn.close();
    	  
    	  }catch(IOException e){
    		  e.printStackTrace(System.err);	}
      }*/
   }
